//
//  SHRedPacketUseView.m
//  youjian03
//
//  Created by Dadeso on 14/11/18.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHRedPacketUseView.h"

@implementation SHRedPacketUseView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//初始化方法
- (id) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UILabel * label = [[UILabel alloc] initWithFrame:SHRectMake(15, 24, self.frame.size.width - 30 ,    15)];
        label.text = @"可用红包金额：";
        label.textColor = [SHUtilitiesTool colorWithHexString:@"#999999"];
        label.font = [UIFont systemFontOfSize:15.0f];
        label.textAlignment = NSTextAlignmentLeft;
        label.backgroundColor = [UIColor clearColor];
        [self addSubview:label];
        self.packetUserMoneyLabel = label;
        
        UITextField * textfield = [[UITextField alloc] initWithFrame:SHRectMake(label.frame.origin.x, label.frame.origin.y + label.frame.size.height + 11.5, label.frame.size.width - 30, 41)];
        textfield.delegate = self;
        textfield.placeholder = @"请填写运费金额";
        textfield.textColor = [SHUtilitiesTool colorWithHexString:@"#e33531"];
        textfield.keyboardType = UIKeyboardTypeNumberPad;
        textfield.borderStyle = UITextBorderStyleRoundedRect;
        textfield.backgroundColor = [SHUtilitiesTool colorWithHexString:@"#ffffff"];
        textfield.layer.borderColor = [[UIColor redColor] CGColor];
        textfield.layer.borderWidth = 1.0f;
        textfield.layer.cornerRadius = 6;
        [self addSubview:textfield];
        self.packetUseMoneyTextField = textfield;
        
        UILabel * label1 = [[UILabel alloc] initWithFrame:SHRectMake(textfield.frame.origin.x + textfield.frame.size.width + 15, textfield.frame.origin.y + 12, 30, 17)];
        label1.textColor = [SHUtilitiesTool colorWithHexString:@"#666666"];
        label1.font = [UIFont systemFontOfSize:17.0f];
        label1.textAlignment = NSTextAlignmentLeft;
        label1.backgroundColor = [UIColor clearColor];
        label1.text = @"元";
        [self addSubview:label1];
        
        UIView * view = [[UIView alloc] initWithFrame:SHRectMake(0, textfield.frame.origin.y + textfield.frame.size.height + 24, self.frame.size.width, 0.5)];
        view.backgroundColor = [SHUtilitiesTool colorWithHexString:@"#cccccc"];
        [self addSubview:view];
        
        UIView * view1 = [[UIView alloc] initWithFrame:SHRectMake(self.frame.size.width / 2, view.frame.origin.y, 0.5, self.frame.size.height - view.frame.origin.y - 0.5)];
        view1.backgroundColor = [SHUtilitiesTool colorWithHexString:@"#cccccc"];
        [self addSubview:view1];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = SHRectMake(0, view.frame.origin.y + 0.5, self.frame.size.width / 2, view1.frame.size.height);
        [btn setTitle:@"确认支付" forState:UIControlStateNormal];
        btn.tag = 1118;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[SHUtilitiesTool colorWithHexString:@"#cccccc"] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:17.0f]];
        [self addSubview:btn];
        
        UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = SHRectMake(self.frame.size.width / 2 + 0.5 , view.frame.origin.y + 0.5, self.frame.size.width / 2 - 0.5, view1.frame.size.height);
        btn1.tag = 1119;
        [btn1 setTitle:@"取消" forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn1 setTitleColor:[SHUtilitiesTool colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        [btn1.titleLabel setFont:[UIFont systemFontOfSize:17.0f]];
        [self addSubview:btn1];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldIsChange) name:UITextFieldTextDidChangeNotification object:nil];
    }
    return self;
}

-(void)btnClick:(UIButton *)btn{
    if (btn.tag == 1118) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    }else if (btn.tag == 1119){
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    }
}

-(void)textFieldIsChange{
    if ([self.packetUseMoneyTextField.text intValue] > 0) {
        UIButton * btn = (UIButton *)[self viewWithTag:1118];
        [btn setTitleColor:[SHUtilitiesTool colorWithHexString:@"#e33531"] forState:UIControlStateNormal];
    }else{
        UIButton * btn = (UIButton *)[self viewWithTag:1118];
        [btn setTitleColor:[SHUtilitiesTool colorWithHexString:@"#cccccc"] forState:UIControlStateNormal];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{

}

@end
